//
//  UIViewController+EL.m
//  jobClient
//
//  Created by 一览iOS on 2017/12/12.
//  Copyright © 2017年 YL1001. All rights reserved.
//

#import "UIViewController+EL.h"
#define El_Nav_BigFont                      [UIFont systemFontOfSize:12]
#define EL_Nav_DefaultFontColor             EL_HexColor(0x222222)
#define EL_Nav_NoSelectedFontColor          EL_HexColor(0xb2b2b2)
#define EL_Nav_DefaultFont                  [UIFont systemFontOfSize:15 weight:UIFontWeightLight]
#define EL_Nav_TitleFont                    [UIFont systemFontOfSize:17 weight:UIFontWeightMedium]
#define EL_Nav_TitleFontColor               [UIColor blackColor]
#define EL_Nav_BackGroupColor               EL_HexColor(0xffffff)
@implementation UIViewController (EL)

- (UILabel *)setNavTitleWithText:(NSString *)text {
    return [self setNavTitleWithText:text withColor:EL_Nav_TitleFontColor];
}

- (UILabel *)setNavTitleWithText:(NSString *)text withColor:(UIColor *)color {
    UILabel *label = [[UILabel alloc] init];
    label.font = EL_Nav_TitleFont;
    label.text = text;
    label.textColor = color;
    label.textAlignment = NSTextAlignmentCenter;
    
    ELBaseTitleView *view = [[ELBaseTitleView alloc] initWithFrame:CGRectMake(0,0,ScreenWidth-160,44)];
    view.backgroundColor = [UIColor clearColor];
    view.intrinsicContentSize = view.frame.size;
    
    [view addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.centerY.equalTo(view);
        make.width.mas_lessThanOrEqualTo(ScreenWidth-160);
    }];
    
    self.navigationItem.titleView = view;
    return label;
}

- (void)setNavTitleWithView:(UIView *)view {
    self.navigationItem.titleView = view;
}

- (UIButton *)setSearchTitleWithText:(NSString *)text WithWidth:(CGFloat)width {
    
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 26,width, 30)];
    searchBtn.layer.cornerRadius = 15;
    searchBtn.layer.masksToBounds = YES;
    searchBtn.backgroundColor = UIColorFromRGB(0xebebeb);
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    searchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -12, 0, 0);
    [searchBtn setTitle:text forState:UIControlStateNormal];
    
    [searchBtn setTitleColor:UIColorFromRGB(0x888888) forState:UIControlStateNormal];
    [searchBtn setImage:[UIImage imageNamed:@"yewen_top_search"] forState:UIControlStateNormal];
    self.navigationItem.titleView = searchBtn;
    
    return searchBtn;
}

- (UIButton *)setButtonTitleWithText:(NSString *)text wdith:(CGFloat)width {
    UIButton *navTitleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    navTitleBtn.frame = CGRectMake(0, 0,width, 40);
    [navTitleBtn setTitleColor:EL_Nav_TitleFontColor forState:UIControlStateNormal];
    navTitleBtn.titleLabel.font = EL_Nav_TitleFont;
    self.navigationItem.titleView = navTitleBtn;
    return navTitleBtn;
}

- (void)setNavBarMainColor {
    if (@available(iOS 13.0, *)) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    } else {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }

    [self.navigationController.navigationBar setBackgroundImage:EL_NavBarBgLightImage forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = NO;//NO
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}


#pragma mark - 导航左右按钮相关

- (UIButton *)getNewButtonWithText:(NSString *)text {
    CGSize size = [text sizeNewWithFont:EL_Nav_DefaultFont];
    CGFloat width = size.width < 40 ? 40:size.width;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setExclusiveTouch:YES];
    button.frame = CGRectMake(0,0,width,40);
    button.backgroundColor = [UIColor clearColor];
    [button setImage:nil forState:UIControlStateNormal];
    [button setTitle:text forState:UIControlStateNormal];
    [button setTitleColor:EL_Nav_DefaultFontColor forState:UIControlStateNormal];
    button.titleLabel.font = EL_Nav_DefaultFont;
    
    return button;
}

- (UIButton *)getNewButtonWithImageName:(NSString *)imageName {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setExclusiveTouch:YES];
    button.frame = CGRectMake(0,0,30,40);
    button.backgroundColor = [UIColor clearColor];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setTitle:@"" forState:UIControlStateNormal];
    [button setTitleColor:EL_Nav_DefaultFontColor forState:UIControlStateNormal];
    button.titleLabel.font = EL_Nav_DefaultFont;
    return button;
}

- (void)setNavLeftButtonWithArr:(NSArray *)viewArr {
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    if (iOS11 == NO) {
        [arr addObject:[self fixedSpaceWithWidth:-12]];
    }
    for (UIView *view in viewArr) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];
        [arr addObject:item];
    }
    self.navigationItem.hidesBackButton = YES;
    self.navigationController.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.leftBarButtonItems = arr;
}

- (void)setNavRightButtonWithArr:(NSArray *)viewArr {
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    if (iOS11 == NO) {
        [arr addObject:[self fixedSpaceWithWidth:-12]];
    }
    for (UIView *view in viewArr) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];
        [arr addObject:item];
    }
    self.navigationItem.rightBarButtonItem = nil;
    self.navigationItem.rightBarButtonItems = arr;
}

- (UIBarButtonItem *)fixedSpaceWithWidth:(CGFloat)width {
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                               target:nil
                                                                               action:nil];
    fixedSpace.width = width;
    return fixedSpace;
}

@end
